from django.http import HttpResponse
from django.shortcuts import render


def upload_file(request):
    if request.method == 'GET':
        return render(request, 'up.html')
    elif request.method == "POST":
        # 获取请求参数
        uname = request.POST.get('uname', '')
        photo = request.FILES.get('photo', '')
        print(photo.name)
        import os
        if not os.path.exists('media'):
            os.makedirs('media')

        with open(os.path.join('web/media', photo.name), 'wb') as fw:
            # 一次性读取文件
            fw.write(photo.read())
            # 分块读取文件
            # for w in photo.chunks():
            #     fw.write(w)
        return HttpResponse('访问成功')
    else:
        return HttpResponse('hello')
